<template>
  <div class="home">
    <div class="header">
      <div class="search">
        <span>标签</span>
        <el-select v-model="header.tag" placeholder="请选择">
          <el-option
            v-for="item in header.option"
            :label="item.label"
            :value="item.value"
            :key="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="search">
        <span>关键字</span>
        <el-input v-model="header.title" placeholder="请输入内容" @input="handleInput"></el-input>
      </div>
      <div class="search">
        <el-button type="primary" @click="search()">搜索</el-button>
        <el-button type="primary" @click="add(1)">新建</el-button>
      </div>
    </div>
    <div class="list-box">
      <div v-for="item in list" :key="item.id" class="item">
        <div class="title" @click="toDetail(item)">{{ item.title }}</div>
        <div class="dept" @click="toDetail(item)">{{ item.dept }}</div>
        <div>
          <span class="mr10">{{ item.author }}</span> <span class="mr10">{{ formatTime(item.updateAt) }}</span>
          <span v-for="tag in formatTag(item.tag)" :key="tag" class="tags">{{ tag }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, computed, ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getDataApi } from '@/api/km'
import { debounce, throttle } from '@/utils/index.js'
import dayjs from 'dayjs'
const router = useRouter()
const add = (id: number) => {
  router.push({ path: '/knowledge/new', query: {} })
}
const list = ref<any>([])
const param = reactive({
  pageNo: 1,
  pageSize: 1000
})
const initData = async () => {
  let params = {
    ...param,
    title: header.title,
    tag: header.tag
  }
  const res = await getDataApi(params)
  if (res.code === 200) {
    list.value = res.data.list
  }
}
const toDetail = (item) => {
  router.push({ path: '/knowledge/detail', query: { id: item.id } })
}
//计算属性 代替 vue2 filter 格式化时间
const formatTime = computed(() => {
  return (time: string) => {
    return dayjs(time).format('YYYY-MM-DD HH:mm')
  }
})
const formatTag = computed(() => {
  return (Tag: String) => {
    return Tag.split(',')
  }
})
//头部搜索
const header = reactive({
  title: '',
  tag: '',
  option: [
    {
      label: '前端',
      value: '前端'
    },
    {
      label: 'VUE2',
      value: 'VUE2'
    },
    {
      label: 'VUE3',
      value: 'VUE3'
    },
    {
      label: 'HTML',
      value: 'HTML'
    },
    {
      label: 'JS',
      value: 'JS'
    },
    {
      label: 'CSS',
      value: 'CSS'
    },
    {
      label: '其他',
      value: '其他'
    },
    {
      label: 'node',
      value: 'node'
    }
  ]
})
const search = () => {
  initData()
}
//事件需要做的事情
const todo = async () => {
  let params = {
    ...param,
    title: header.title,
    tag: header.tag
  }
  const res = await getDataApi(params)
  if (res.code === 200) {
    console.log(res.data)
    list.value = res.data.list
  }
}
// const handleInput = debounce(todo, 3000)
const handleInput = throttle(todo, 3000)

onMounted(() => {
  initData()
})
</script>
<style scoped lang="scss">
.header {
  margin: 10px;
  display: flex;
  .search {
    display: flex;
    width: 25%;
    align-items: center;
    margin-right: 40px;
    span {
      width: 65px;
    }
  }
}
.list-box {
  .item {
    padding: 20px;
    margin: 20px;
    border-bottom: 1px solid #ede6e678;
    .title {
      font-size: 16px;
      font-weight: 600;
      cursor: pointer;
    }
    .dept {
      margin: 20px 0;
      cursor: pointer;
    }
    .tags {
      margin-left: 20px;
    }
  }
}
</style>
